package com.cskaoyan.service.ad.admin;

import com.cskaoyan.bean.Ad;
import com.cskaoyan.bean.AdExample;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.bean.ad.CreateAdBO;
import com.cskaoyan.bean.ad.DeleteAdBO;
import com.cskaoyan.bean.ad.UpdateAdBO;
import com.cskaoyan.mapper.AdMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class AdServiceImpl implements AdService {

    @Autowired
    AdMapper adMapper;

    /**
     * 已测试
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param name
     * @param content
     * @return
     */
    @Override
    public ListData<Ad> list(String page, String limit, String sort, String order, String name, String content) {

        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(limit));
        AdExample adExample = new AdExample();
        adExample.setOrderByClause(sort +" " + order);
        AdExample.Criteria criteria = adExample.createCriteria();
        if (name != null){
            criteria.andNameLike("%"+name+"%");
        }
        if (content != null){
            criteria.andContentLike("%"+content+"%");
        }
        List<Ad> adList = adMapper.selectByExample(adExample);
        ListData<Ad> listData = new ListData<>();
        listData.setItems(adList);

        PageInfo<Ad> pageInfo = new PageInfo<>(adList);
        long total = pageInfo.getTotal();
        listData.setTotal((int) total);

        return listData;
    }

    /**
     * 未测试
     * @param createAdBO
     * @return
     */
    @Override
    public boolean create(CreateAdBO createAdBO) {
        Ad ad = new Ad();
        ad.setName(createAdBO.getName());
        ad.setContent(createAdBO.getContent());
        ad.setUrl(createAdBO.getUrl());
        ad.setLink(createAdBO.getLink());
        ad.setPosition(createAdBO.getPosition());
        ad.setEnabled(true);
        int insert = adMapper.insert(ad);
        return 1 == insert;
    }

    /**
     * 已测试
     * @param deleteAdBO
     * @return
     */
    @Override
    public boolean delete(DeleteAdBO deleteAdBO) {
        int i = adMapper.deleteByPrimaryKey(deleteAdBO.getId());
        return 1 == i;
    }

    /**
     *
     * @param updateAdBO
     * @return
     */
    @Override
    public boolean update(UpdateAdBO updateAdBO) {
        System.out.println("updateAdBO.getUpdateTime() = " + updateAdBO.getUpdateTime());
        Ad ad = new Ad();
        ad.setId(updateAdBO.getId());
        ad.setAddTime(null);
        ad.setContent(updateAdBO.getContent());
        ad.setEnabled(updateAdBO.isEnabled());
        ad.setLink(updateAdBO.getLink());
        ad.setName(updateAdBO.getName());
        ad.setPosition(updateAdBO.getPosition());
        ad.setUpdateTime(updateAdBO.getUpdateTime());

        ad.setUrl(updateAdBO.getUrl());
        int i = adMapper.updateByPrimaryKeySelective(ad);
        return 1 == i;
    }

    @Override
    public int getLastInsertId() {
        int maxId = adMapper.getMaxId();
        return maxId;
    }

    @Override
    public Ad selectByPrimaryKey(int lastInsertId) {
        Ad ad = adMapper.selectByPrimaryKey(lastInsertId);
        return ad;
    }


}
